package com.twu.biblioteca.model;

import com.twu.biblioteca.exceptions.BookAlreadyReservedException;
import com.twu.biblioteca.exceptions.InexistantBookException;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: cschlumb
 * Date: 05/08/2013
 * Time: 18:30
 * To change this template use File | Settings | File Templates.
 */
public class BookList {

    private static BookList instance = null;

    HashMap<Book, Boolean> bookList = new HashMap<Book, Boolean>();

    private BookList(){}

    public static BookList getInstance(){
        if (instance == null){
            instance = new BookList();
        }

        return instance;
    }

    public void add(Book book){
        bookList.put(book, false);
    }

    public int size(){
        return bookList.size();
    }

    public void addList(List<Book> listOfBooks){
        for (Book book : listOfBooks){
            bookList.put(book, false);
        }
    }

    public void displayList(IBookFormatter bookFormatter){
        for (Map.Entry<Book, Boolean> e : bookList.entrySet()){
            bookFormatter.addBook(e.getKey());
        }

        bookFormatter.print();
    }

    public void clear(){
        bookList.clear();
    }

    public void reserve(UUID uuid) throws BookAlreadyReservedException, InexistantBookException{

        for (Map.Entry<Book, Boolean> e :bookList.entrySet()){
            if (e.getKey().getUuid().equals(uuid)){
                if (e.getValue() != true){
                    bookList.put(e.getKey(), true);
                    return;
                } else {
                    throw new BookAlreadyReservedException();
                }
            }
        }

        throw new InexistantBookException();
    }
}
